<template>
    <div>
        <el-table :data="cateList" border style="width: 100%" row-key="id" default-expand-all
            :tree-props="{ children: 'children' }">
            <el-table-column prop="id" label="分类编号" width="180">
            </el-table-column>
            <el-table-column prop="catename" label="分类名称" width="180">
            </el-table-column>
            <el-table-column prop="img" label="图片">
                <template slot-scope="item">
                    <div>
                        <!-- 因为图片不是必传项  所有加一个判断 -->
                        <img v-if="item.row.img" class="img" :src="item.row.img" alt="">

                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="status" label="状态">
                <template slot-scope="item">
                    <div>
                        {{ item.row.status == 1 ? '启用' : '禁用' }}
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="item">
                    <div>
                        <el-button type="success" size="small" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" size="small" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
import { mapGetters, mapActions } from 'vuex';
import { cateDelete } from '../../request/api';
export default {

    data() {
        return {
            tableData: [{
                date: '2016-05-02',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1518 弄'
            }, {
                date: '2016-05-04',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1517 弄'
            }, {
                date: '2016-05-01',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1519 弄'
            }, {
                date: '2016-05-03',
                name: '王小虎',
                address: '上海市普陀区金沙江路 1516 弄'
            }]
        }
    },
    methods: {
        ...mapActions({
            getCateList: 'cate/getCatelistAction'
        }),
        // 删除一条数据
        del(id) {
            // 询问用户是否要删除
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                //调取删除接口
                cateDelete({ id }).then(res => {
                    if (res.code == 200) {
                        // 删除成功
                        this.$message.success(res.msg);
                        // 重新渲染视图
                        this.getCateList()
                    } else {
                        // 删除失败
                        this.$message.error(res.msg);
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        // 编辑
        edit(id) {
            this.$emit('edit', id);
        }
    },
    mounted() {
        console.log(this.$store, this.getCateList);
        this.getCateList()
    },
    watch: {},
    computed: {
        ...mapGetters({
            cateList: 'cate/getCateList'
        })
    },
    filters: {},
    components: {},
}
</script>
<style scoped>
.img {
    width: 80px;
    height: 80px;
}
</style>